package editmailbox2;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use base 'createmailbox2', 'editmailbox', 'func';
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);
use strict;
use CGI::Carp qw(fatalsToBrowser);

my $func = func->new();

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub valid_param_mailbox {
    my $self = shift;
    $self->SUPER::valid_param_mailbox(@_);
    return $self;

}

sub valid_param2_mailbox_edit {
    my $self = shift;

    my $user_sql;
    if ( $self->{he}->{fUsername_correct} != 1 ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT t1.quota,
                                  t1.username,
                                  t1.password,
                                  t2.idnane
                         FROM mailbox AS t1,
                              domain AS t2 
                         WHERE t1.username = ? 
                         AND t2.domain = ?
                         AND t2.domain = t1.domain
                         LIMIT 1  
                             '
        );
        $sth->execute( $self->{p}->{fUsername} . '@' . $self->{p}->{fDomain},
            $self->{p}->{fDomain} );
        $user_sql = $sth->fetchrow_hashref();

        $sth->finish();

        if ( !defined( $user_sql->{username} ) ) {
            $self->{he}->{fusername_correct_exist} = 1;
        }

        if ( defined( $user_sql->{idnane} ) ne $self->{session}->{user}
            && $self->{session}->{name} ne 'Admin' )
        {
            $self->{he}->{fDomain_correct_exist} = 1;
        }
    }

    if (   ( $self->{session}->{name} ne 'Admin' )
        && ( $self->{he}->{fDomain_correct} != 1 ) )
    {

        if (   ( $self->{dd}->{mailboxes} != 0 )
            && ( $self->{dd}->{c_mailbox} != 0 )
            && ( $self->{dd}->{mailboxes} < $self->{dd}->{c_mailbox} ) )
        {
            $self->{he}->{MAILBOX_LIMIT} = 1;
        }

        if (   ( $self->{dd}->{maxquota} != 0 )
            && ( $self->{dd}->{maxquota} < $self->{p}->{fQuota} )
            && ( $self->{p}->{fQuota} != 0 ) )
        {
            $self->{he}->{MAILBOX_LIMITMB} = 1;
        }

        if (
               ( $self->{dd}->{mb_domain} > 0 )
            && ( $self->{dd}->{mb_all_mailbox} != 0 )
            && ( $self->{dd}->{mb_all_mailbox} > 0 )
            && ( $self->{dd}->{mb_domain} < $self->{dd}->{mb_all_mailbox} +
                $self->{p}->{fQuota} -
                $user_sql->{quota} )
          )
        {
            $self->{he}->{MAILBOX_ALL_LIMITMB} = 1;
        }
    }

    if ( $self->{session}->{name} eq 'Admin' && $self->{p}->{fPassword} ne '' )
    {

        if (
               ( !defined( $self->{p}->{fPassword} ) )
            || ( 100 < length( $self->{p}->{fPassword} ) )
            || ( $self->{se}->{max_long_passwd} >
                length( $self->{p}->{fPassword} ) )
            || ( $self->{p}->{fPassword} !~ /\w+/ )
          )
        {
            $self->{he}->{fPassword_correct} = 1;
        }

        if ( $self->{p}->{fPassword} ne $self->{p}->{fPassword2} ) {
            $self->{he}->{fPassword2_correct} = 1;
        }
    }

    if ( $self->{session}->{name} ne 'Admin' ) {

        if (
               ( !defined( $self->{p}->{fPassword} ) )
            || ( 100 < length( $self->{p}->{fPassword} ) )
            || ( $self->{se}->{max_long_passwd} >
                length( $self->{p}->{fPassword} ) )
            || ( $self->{p}->{fPassword} !~ /\w+/ )
          )
        {
            $self->{he}->{fPassword_correct} = 1;
        }

        if ( $self->{p}->{fPassword} ne $self->{p}->{fPassword2} ) {
            $self->{he}->{fPassword2_correct} = 1;
        }
    }

    $self->{p}->{username_sql} = $user_sql->{password};
    return $self;
}

sub edit_mailbox {

    my $self = shift;

    my ( $pwc, $maildir, $salt );
    my @smtp_out = ();

    my $fount;
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $fount = 1; last; }
    }

    if ( !$fount ) {

        if ( $self->{se}->{type_passwd} eq 'md5crypt' ) {
            $self->{p}->{fPassword} =
              $func->md5crypt( $self->{p}->{fPassword} );
        }

        $self->{p}->{email_smtp} =
          $self->{p}->{fUsername} . '@' . $self->{p}->{fDomain};

        my @insql = ();
        if (   $self->{session}->{name} eq 'Admin'
            && $self->{p}->{fPassword} eq '' )
        {
            push @insql, $self->{p}->{username_sql};

        }
        else {
            push @insql, $self->{p}->{fPassword};
        }

        push @insql, $self->{p}->{fName}, $self->{p}->{fQuota},
          $self->{p}->{fActive}, $self->{p}->{email_smtp},
          $self->{p}->{fDomain};

        my $sth = $self->{dbh}->prepare(
            'UPDATE mailbox 
                            SET password = ?,
                            name = ?,
                            quota = ?,
                            modified = DATETIME("NOW"),
                            active = ? 
                            WHERE username = ?
                            AND domain = ?
            '
        );

        $sth->execute(@insql);
        $sth->finish();

        if ( $self->{p}->{fMail} eq 'on' ) {
            $self->SUPER::sent_email(@_);

        }
        $self->{he}->{mailbox_added} = 1;
        $self->{he}->{EMAIL_ADD}     = $self->{p}->{email_smtp};
    }

    return $self;
}

sub return_edit_mailbox {
    shift->SUPER::return_edit_mailbox(@_);
}

sub templ_hash_mailbox {
    my $self = shift;
    $self->{session}->{filet} = 'edit-mailbox';
    $self->SUPER::templ_hash(@_);
    return $self;
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
